//如何右键能够直接创建？？扩展编辑器功能
Shader "parallelMapping视差映射.top/Template"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        _NormalMap("NormalMap",2D) = "bump"{}
        _ParallaxMap("ParallaxMap",2D) = "white"{}
        _ParallaxScale("ParallaxScale",Range(0.01,0.05)) = 0.02
    }
    SubShader
    {
        Tags 
        { 
            "Queue"="Geometry"
            "RenderPipeline" = "UniversalRenderPipeline"//改造1
        }
        // LOD 100
        Pass
        {
            Name "Unlit"
            Tags
            {
                "LightMode"="ForwardBase"//改造6
            }
            HLSLPROGRAM//改造2
            #pragma vertex vert
            #pragma fragment frag
            // #include "UnityCG.cginc"
            // #include "Lighting.cginc"
            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"//改造3
            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"//改造3

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
                float3 normal : NORMAL;
            }

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 pos : SV_POSITION;
                float3 worldNormal : TEXCOORD1;
                float3 worldPos : TEXCOORD2;
            }
            sampler2D _MainTex;
            float4 _MainTex_ST;

            //计算uv偏移delta
            inline float2 ParallaxUvDelta(v2f i)
            {
                //高度图描述高度数据
                half h = tex2D(_ParallaxMap,i.uvMain).r;
                //切线空间中视线方向
                half3 viewDir = normalize(_WorldSpaceCameraPos.xyz - i.worldPos.xyz);
                //将三维的视线向量投影到二维的uv平面，乘高度数据，_ParallaxScale是一个用户可调节的值
                // 根据效果需要进行调节，数值太大会造成视觉错误
                float2 delta = viewDir.xy / viewDir.z   * (h * _ParallaxScale);
                return delta;

            }


            v2f vert (appdata v)
            {
                v2f o;
                // o.vertex = UnityObjectToClipPos(v.vertex);
                o.pos = TransformObjectToHClip(v.vertex);//改造4
                // o.uv = TRANSFORM_TEX(v.uv, _MainTex);
                // o.worldNormal = UnityObjectToWorldNormal(v.normal);
                // o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
                return o;   
            }

            half2 ParallaxSimple(input IN){
                return IN.uv_MainTex;
            }
            // fixed4 frag (v2f i) : SV_Target
            half4 frag (v2f i) : SV_Target //改造5
            {
                // fixed3 worldNormal = normalize(i.worldNormal);
                // fixed3 worldLightDir = normalize(UnityWorldSpaceLightDir(i.worldPos));
                fixed4 col = tex2D(_MainTex, i.uv);//改造5
                // fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.rgb * albedo;
                // fixed3 diffuse = _LightColor0.rgb * albedo * max(0, dot(worldNormal, worldLightDir));
                // fixed3 color = ambient + diffuse;
                return col;

//简单的surfaceshader
                half2 uvDelta =  ParallaxSimple(IN);
                half2 uvMain = IN.uv_MainTex + uvDelta;
                o.Normal = tex2D(_NormalMap,uvMain).rgb;

            }
            ENDHLSL //改造2
        }
    }
}